var uriUtil = require('../../utils/uri.js');
var header = uriUtil.header
Page({
  data: {
    baseUrl: uriUtil.BASE_URL,
    userInfo:{
      username: '',
      password: '',
      name: '',
      gender: 1,
      age: '',
      height: '',
      weight: '',
      target: '',
      intake:'',
      bmi:'',
    },
    isModify: false
  },
  //监听页面加载
  onLoad: function (options) {
    var that = this;
    let userInfo = uriUtil.getUserInfo();
    if(userInfo){
      that.setData({
        userInfo:{
          id: userInfo['id'],
          username: userInfo['username'],
          password: userInfo['password'],
          name: userInfo['name'],
          gender: userInfo['gender'],
          age: userInfo['age'],
          avatarUrl: userInfo['avatarUrl'],
          height: userInfo['height'],
          weight: userInfo['weight'],
          target: userInfo['target'],
          intake: userInfo['intake'],
          bmi:userInfo['bmi']
        },
        isModify: true
      })
    }
  },
  //注册提交
  regist: function(){
    let that = this;
    let userInfo = that.data.userInfo;
    let url = uriUtil.BASE_URL;
    this.BMI();
    this.intakeOut();
    if(that.data.isModify){   
     url = url+uriUtil.userUpdate;//修改资料
    }else{
     url = url+uriUtil.userAdd;//注册资料
    }
    wx.request({
      url: url,
      header: header,
      data:userInfo,
      method: 'post',
      success: function (res){
        if (res.data==1) {
          wx.request({
            url: uriUtil.BASE_URL+uriUtil.userByName,
            method:'get',
            data:{
              username : userInfo.username
            },
            success:function(res){
              wx.showToast({
                  title: '成功',
                });
              uriUtil.setUserInfo(res.data[0]);
              wx.switchTab({
                url: '../mine/mine',
              })
            }
          })
        }},
      });},
  //输入处理
  onInput: function(e){
    let typename = e.currentTarget.dataset.typename;
    let value = e.detail.value;
    this.setUserinfoData(typename,value);
  },
  //性别选择
  onSexChange: function(e){
    let value = e.detail.value;
    this.setUserinfoData('gender', value);
  }, 
  //BMI计算
  BMI:function(){
    let that = this
    let height = that.data.userInfo.height
    let weight = that.data.userInfo.weight
    let bmi = (weight/(height*height)).toFixed(1)
    if(bmi<18.5||bmi==18.5){
      bmi = '过轻'
    }else if(bmi<24&&bmi>18.5){
      bmi = '健康'
    }else if(bmi<27.9&&bmi>24){
      bmi = '微胖'
    }else{
      bmi = '过重'
    }
    that.setUserinfoData('bmi', bmi);
  },
  //基础热量计算
  intakeOut:function () {
    let that = this;
    let userInfo = that.data.userInfo;
    let intak = 0;
    if(userInfo.gender==1){
      intak = 66.5+13.8*userInfo.weight+5*userInfo.height-6.8*userInfo.age;
    }else{
      intak = 665.1+9.6*userInfo.weight+1.8*userInfo.height-4.7*userInfo.age;
    }
    intak = intak.toFixed(2)
    this.setUserinfoData('intake', intak);
  },
  //赋值userInfo
  setUserinfoData: function(key,value){
    let userInfo = this.data.userInfo;
    userInfo[key] = value;
    this.setData({
      userInfo: userInfo
    });
  },
})